﻿using System.Collections.Generic;
using System.Linq;
using HeritageAcademy.Models.ViewModels;

namespace HeritageAcademy.Models.Filters
{
    public class CourseTitlesFilter : FilteredCourseFilter
    {
		#region Parameters 
        private List<CourseFilter> _SubFilters = new List<CourseFilter>();
		#endregion

		#region Ctor 
        public CourseTitlesFilter(LibraryViewModel library) : base(library) { }
		#endregion

		#region Properties 
        public override string DisplayText
        {
            get { return "Темы"; }
        }

        public override bool HasSubFilters
        {
            get { return true; }
        }

        public override List<CourseFilter> SubFilters
        {
            get { return _SubFilters; }
        }

		#endregion Properties 
	
        #region Public Methods 
        public override void RefreshSubFilters()
        {
            var results = Library.Courses.Select(item => item.Model.Subject)
                .Distinct()
                .OrderBy(subject => subject);

            var filters = results.ToList().ConvertAll(subjectString => (CourseFilter)new SingleCourseTitleFilter(Library, subjectString));

            filters.Insert(0, new AllCoursesFilter(Library));

            _SubFilters = filters;
        }
		#endregion Public Methods 
    }

    public class SingleCourseTitleFilter : CourseFilter
    {
		#region Ctor 
        public SingleCourseTitleFilter(LibraryViewModel library, string subject)
            : base(library)
        {
            Subject = subject;
        }
		#endregion Constructors 

		#region Properties 
        public override string DisplayText
        {
            get { return Subject; }
        }

        public string Subject { get; set; }

        public override bool HasSubFilters
        {
            get { return false; }
        }
		#endregion Properties 

		#region Public Methods 
        public override void RefreshResults()
        {
            var results = Library.Courses.Where(item => item.Model.Subject.Equals(Subject));
            Results = new List<CourseViewModel>(results);
        }
		#endregion Public Methods 
    }

}
